<template>
  <div class="app-container">
    <el-tabs v-model="tabIndex" @tab-click="changTab">
      <el-tab-pane label="商品" name="0"></el-tab-pane>
      <el-tab-pane label="配件" name="1"></el-tab-pane>
      <el-tab-pane label="物料" name="2"></el-tab-pane>
    </el-tabs>
    <el-form :model="form" ref="form" :inline="true">
      <el-form-item label="产品名称" prop="productName">
        <el-input v-model="form.productName" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="产品类型" prop="productCategoryId">
        <treeselect v-model="form.productCategoryId" :options="goodTypeList" :show-count="true"
                    placeholder="请选择" :normalizer="normalizer" style="width: 200px"/>
      </el-form-item>
      <el-form-item label="产品品牌" prop="brandId">
        <el-select v-model="form.brandId" filterable clearable>
          <el-option
            v-for="item in brandList"
            :key="item.id"
            :label="item.brandName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="产品来源" prop="productSource">
        <el-select v-model="form.productSource" filterable clearable>
          <el-option
            v-for="item in dict.type.basic_good_source"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" plain icon="el-icon-menu" @click="listShow=false" v-if="listShow">图标</el-button>
    <el-button type="primary" plain icon="el-icon-menu" @click="listShow=true" v-if="!listShow">列表</el-button>
    <el-button v-hasPermi="['ims:goodMng:add']" type="success" plain icon="el-icon-plus" @click="updateRow(0,{})">新增
    </el-button>
    <!--    <el-button  v-hasPermi="['ims:goodMng:import']" type="primary" plain icon="el-icon-upload" @click="updateRow(4)">导入</el-button>-->
    <el-button v-hasPermi="['ims:goodMng:export']" type="warning" plain icon="el-icon-download"
               @click="updateRow(5,{})">导出
    </el-button>
    <el-table
      v-if="listShow"
      v-loading="loading"
      :data="list"
    >
      <el-table-column type="index" label="序号" align="center">
        <template slot-scope="scope">
          <span>{{ (form.pageNum - 1) * form.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="imageUrl" label="图片" align="center" width="200">
        <template slot-scope="scope">
          <div v-if="!scope.row.imageUrl">暂无</div>
          <el-image
            v-else
            style="width: 80px;height: 80px"
            :src="scope.row.imageUrl"
            fit="contain"
            :preview-src-list="[scope.row.imageUrl]"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="productName" :label="typeList[tabIndex]+'名称'" align="center">
        <template slot-scope="scope">
          <el-link type="primary" @click="updateRow(1,scope.row)">{{ scope.row.productName }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="productCode" :label="typeList[tabIndex]+'编码'" align="center" width="200"/>
      <el-table-column prop="productCategoryName" :label="typeList[tabIndex]+'类型'" align="center" width="200"/>
      <el-table-column prop="brandName" :label="typeList[tabIndex]+'品牌'" align="center" width="200"/>
      <el-table-column prop="productSource" :label="typeList[tabIndex]+'来源'" align="center" width="200">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.basic_good_source" :value="scope.row.productSource"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="updateRow(1,scope.row)"
          >详情
          </el-button>
          <el-button
            type="text"
            v-hasPermi="['ims:goodMng:update']"
            @click="updateRow(2,scope.row)"
          >修改
          </el-button>
          <el-button
            type="text"
            v-hasPermi="['ims:goodMng:attr']"
            @click="updateRow(6,scope.row)"
          >属性
          </el-button>
          <el-button
            type="text"
            v-hasPermi="['ims:goodMng:delete']"
            @click="updateRow(3,scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div v-if="!listShow">
      <div v-for="(val,index) in list" class="imgDiv" @click="updateRow(1,val)">
        <div class="numDiv">
          {{ (form.pageNum - 1) * form.pageSize + index + 1 }}
        </div>
        <el-image
          style="width: 100%; height: 100px"
          :src="val.imageUrl"
          fit="contain"
        >
          <div slot="error" style="font-size: 15px;line-height: 120px;text-align: center">暂无图片</div>
        </el-image>
        <div class="unitDiv">
          <div class="textDiv">名称：{{ val.productName }}</div>
          <div class="textDiv">品牌：{{ val.brandName || '-' }}</div>
          <div class="textDiv">分类：{{ val.productCategoryName || '-' }}</div>
        </div>
      </div>
    </div>
    <pagination :total="total" :page.sync="form.pageNum" :limit.sync="form.pageSize" @pagination="getList"/>
    <updateDialog ref="updateDialog" :goodTypeList="goodTypeList" @ok="handleQuery"></updateDialog>
    <attrDialog ref="attrDialog" :goodTypeList="goodTypeList" @ok="handleQuery"></attrDialog>
    <tempDialog ref="tempDialog"></tempDialog>
  </div>
</template>

<script>
import {listGood, listGoodType, deleteGood, listBrand} from "../../../api/imsMng";
import tempDialog from "../../../myComp/tempDialog/tempDialog.vue";
import updateDialog from "./updateDialog.vue";
import attrDialog from "./skuDialog.vue";
import Treeselect from "@riophae/vue-treeselect";
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import {formatDay} from "../../../utils";

export default {
  name: 'Good',
  dicts: ['basic_good_source'],
  components: {tempDialog, updateDialog, attrDialog, Treeselect},
  data() {
    return {
      loading: false,
      listShow: true,
      tabIndex: 0,
      goodTypeList: [],
      brandList: [],
      list: [],
      typeList: ['商品', '配件', '物料'],
      form: {
        productName: '',
        productCategoryId: null,
        brandId: '',
        productSource: '',
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      normalizer(node) {
        return {
          id: node.id,
          label: node.productCategoryName,
          children: node.productCategoryVoList,
        }
      },
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      this.handleQuery()
      listGoodType().then(res => {
        this.goodTypeList = res.data
      })
      listBrand().then(res => {
        this.brandList = res.rows
      })
    },
    changTab(e) {
      this.tabIndex = e.index
      this.handleQuery()
    },
    handleQuery() {
      this.form.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('form')
      this.handleQuery()
    },
    getList() {
      this.form.productType = Number(this.tabIndex) + 1
      this.loading = true
      listGood(this.form).then(res => {
        this.list = res.rows
        this.total = res.total
      }).finally(e => {
        this.loading = false
      })
    },
    updateRow(flag, row) {
      let form = JSON.parse(JSON.stringify(row))
      if (flag == 0) {
        this.$refs.updateDialog.show(flag, Number(this.tabIndex) + 1, {})
      }
      if (flag == 1 || flag == 2) {
        this.$refs.updateDialog.show(flag, Number(this.tabIndex) + 1, form)
      }
      if (flag == 3) {
        this.$modal.confirm('是否确定删除').then(() => {
          deleteGood(row.id).then(res => {
            this.handleQuery()
            this.$message.success('删除成功')
          })
        })
      }
      if (flag == 4) {
        this.$refs.tempDialog.show()
      }
      if (flag == 6) {
        this.$refs.attrDialog.show(form)
      }
      if (flag == 5) {
        this.download('/ims/product/export', {
          ...this.form
        }, `产品管理_${formatDay(new Date())}.xlsx`)
      }
    },
  }
}
</script>
<style scoped>
.imgDiv {
  width: 200px;
  height: 200px;
  border: 1px dashed #999;
  padding: 10px 0;
  border-radius: 10px;
  display: inline-block;
  margin: 10px 50px;
  cursor: pointer;
  border-radius: 10px;
  position: relative;

}
.numDiv{
  position: absolute;
  left: 10px;
  top: 0;
  font-size: 15px;
  font-weight: bold;
}
.imgDiv:hover {
  transform: scale(1.1);
  color:#0275a1;
  border: 1px solid #0275a1;
}

.unitDiv{
  width: 200px;
}
.textDiv {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin: 5px 10px;
}
</style>
